<?php


namespace App\Services;


use App\Jobs\GroupQueue;
use App\Models\Combination;
use App\Models\Commander;
use App\Models\GroupGood;
use App\Models\GroupModel;
use App\Models\OrderModel;
use Godruoyi\Snowflake\Sonyflake;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Nette\Schema\ValidationException;

class SpellRegiment
{
    static public function  SpellRegimentAdd($data,$duration)
    {
        try {
            //获取需要添加的数据
            $stop_time =date('Y-m-d H:i:s',time()+($duration + 86400)) ;
            $row = [
                //团长id
                'uid'=>$data['uid'],
                //拼团商品
                'group_id'=>$data['group_id'],
                //限量
                'limit_quantity'=>$data['limit_quantity'],
                //拼团人数
                'group_number'=>$data['group_number'],
                //该团存在时间
                'duration'=>$duration,
                //结束时间
                'stop_time'=>$stop_time,
                //拼团状态 1是拼团中，2.未开始，3.已结束
                'state'=>1,
            ];
            $combinationPink = Combination::create($row);
            if($combinationPink){
                //当我开团成功的时候获取到限量商品的值，然后去拼团商品进行查询
                $group_number = $combinationPink['group_number'];
                //拼团商品列表查询出限量
                $res = GroupGood::where('id',$combinationPink['group_id'])->get()->toArray();
                //取出
                $limit_quantity = $res[0]['limit_quantity'];
                //进行相减
                $sum = $limit_quantity-$group_number;
                //然后更改限量剩余进行展示
                GroupGood::where('id',$combinationPink['group_id'])->update(['residue_quantity'=>$sum]);
                return Request::success(200,'开团成功','');
            }
        }catch (\Exception $exception){
            return Request::fail(1000,$exception,'');
        }

    }

    /**
     * 用户发起拼团
     */
    public function groupBookingAdd($data,$price)
    {
        try {
            $snow = new Sonyflake();
            $order_number = $snow->id();
            $param = [
                'order_number'=>$order_number,
                'uid'=>$data['id'],
                'group_id'=>$data['group_id'],
                't_id'=>$data['tid'],
                'site'=>$data['pick_site'],
                'price'=>$price,
                'paid'=>0,
                'delivery_type'=>$data['delivery_type'],
                'refund_reason_wap_explain'=>0
            ];
            Redis::lpop('group_goods'.$data['group_id']);

            OrderModel::create($param);

            //用户参团以后获取到目前团内的人数
            $group = Combination::where('uid',$data['tid'])->where('group_id',$data['group_id'])->first();
            //将团内人数+1
            $people = $group['people']+1;
            //将团内人数进行修改
            $update = Combination::where('uid',$data['tid'])->where('group_id',$data['group_id'])->update(['people'=>$people]);
            if($update){
                return Request::success(200,'拼团成功','');
            }
        }catch (\Exception $exception){
            return Request::fail(1000,'拼团失败','');
        }
    }
}